<style lang="scss" scoped>
.forget1 {
  width: 100%;
  height: 100vh;
  background-image: url("@/images/bg/login_bg.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  .forget1-item {
    width: 300px;
    background-color: #fff;
    padding: 20px;
    border-radius: 10px;
  }
}
</style>

<template>
  <div class="forget1">
    <van-nav-bar
      title="忘记密码"
      left-arrow
      @click-left="router.back()"
      style="background-color: #0091ff; width: 100%"
    />
    <div class="forget1-item">
      <el-form :model="formData">
        <el-form-item prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱"></el-input>
          <el-button type="primary" style="position: absolute; right: 0; top: 0" @click="getCode" :disabled="disabled">
            {{ codeMsg }}
          </el-button>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="formData.code" placeholder="请输入验证码"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <van-button block style="width: 300px" @click="router.push('/forget2')">下一步</van-button>
    </div>
    <div></div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { request } from '../../axios/request'
const router = useRouter()

// 表单数据
const formData = ref({
  email: '',
  code: ''
})

// 忘记密码1
const codeMsg = ref('获取验证码')
let disabled = ref(false)
const getCode = async () => {
  if (formData.value.email === '') {
    ElMessage.error('邮箱输入为空')
    return
  }
  // 发送请求获取验证码
  await request
    .post(
      '/api/health/user/v1/sendOutEmailCode',
      {
        email: formData.value.email
      },
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      }
    )
    .then((res) => {
      let { status, message } = res.data
      if (status === '0000') {
        ElMessage.success(message)
        let timer = ref(6)
        disabled.value = true
        let time = setInterval(() => {
          timer.value--
          codeMsg.value = `${timer.value}秒后重新获取`
          if (timer.value === 0) {
            clearInterval(time)
            timer.value = 6
            disabled.value = false
            codeMsg.value = '获取验证码'
          }
        }, 1000)
      } else {
        ElMessage.error(message)
      }
    })
}
</script>   